<?php

class CommentsController extends MainController implements ControllerInterface {

	/**
	 * strona glowna
	 *
	 */
	public function IndexAction($param) {
		
		//search
		if (Request::IsPost()) {
			$param['new'] = urlencode(Request::GetPost('new'));
			$param['type'] = urlencode(Request::GetPost('type'));
		}

		$filter['new'] = isset($param['new']) ? $param['new'] : 0;
		$filter['type'] = isset($param['type']) ? $param['type'] : 0;
		$table_data = $this->getTableData('comments_table', $param, array('o' => 'title', 'v' => 'malejaco', 't' => 10));
		$v = $table_data['v'] == 'malejaco' ? 'DESC' : 'ASC';
		$oTable = array(
			'title' => 'comments.title',
			'type' => 'comments.type',
			'id' => 'comments.id_comments',
			'user' => 'user.email'
		);
		$o = $oTable[$table_data['o']];
		$p = isset($param['strona']) ? $param['strona'] : 1;

		$dalData = CommentsDAL::GetDalDataObj(
						array(
							'join' => array(
								'User' => ' LEFT JOIN user ON user.id_user = comments.id_user'
							))
		);
		
		foreach($filter as $key => $value) {
			if($value)
				$dalData->addCondition($key, $value);
		}
		$dalData->setCount(true);
		$count = CommentsDAL::GetResult($dalData);

		$dalData->setSortBy("$o $v");
		$dalData->setCount(false);
		$dalData->setLimit((($p - 1) * $table_data['t']) . ', ' . $table_data['t']);
		
		$list = CommentsDAL::GetResult($dalData);
		$com = new Comments();
		
		if(isset($param['type']))
		$this->smarty->assign('selected_type', $param['type']);
		$this->smarty->assign('types', $com->getAllTypes());
		$this->smarty->assign('filter', $filter);
		$this->smarty->assign('list', $list);
		$this->smarty->assign('c', $count);
	}
	
	public function moderationAction($param) {
		//search
		if (Request::IsPost()) {
			$param['new'] = urlencode(Request::GetPost('new'));
			$param['type'] = urlencode(Request::GetPost('type'));
		}

		$filter['new'] = isset($param['new']) ? $param['new'] : 0;
		$filter['type'] = isset($param['type']) ? $param['type'] : 0;
		$table_data = $this->getTableData('comments_table', $param, array('o' => 'title', 'v' => 'malejaco', 't' => 10));
		$v = $table_data['v'] == 'malejaco' ? 'DESC' : 'ASC';
		$oTable = array(
			'title' => 'comments.title',
			'type' => 'comments.type',
			'id' => 'comments.id_comments',
			'user' => 'user.email'
		);
		$o = $oTable[$table_data['o']];
		$p = isset($param['strona']) ? $param['strona'] : 1;

		$dalData = CommentsDAL::GetDalDataObj(
						array(
							'join' => array(
								'User' => ' LEFT JOIN user ON user.id_user = comments.id_user',
								'CommentsAbuse' => ' RIGHT JOIN comments_abuse ON comments_abuse.id_comments = comments.id_comments'
							))
		);
		
		foreach($filter as $key => $value) {
			if($value)
				$dalData->addCondition($key, $value);
		}
		$dalData->setCount(true);
		$count = CommentsDAL::GetResult($dalData);

		$dalData->setSortBy("$o $v");
		$dalData->setCount(false);
		$dalData->setLimit((($p - 1) * $table_data['t']) . ', ' . $table_data['t']);
		
		$list = CommentsDAL::GetResult($dalData);
		$com = new Comments();
		
		if(isset($param['type']))
		$this->smarty->assign('selected_type', $param['type']);
		$this->smarty->assign('types', $com->getAllTypes());
		$this->smarty->assign('filter', $filter);
		$this->smarty->assign('list', $list);
		$this->smarty->assign('c', $count);
	}
	
	public function editAction($param) {
		$dalData = new Comments();
		$obj = CommentsDAL::GetResult($dalData->GetDalDataObj(
								array('condition' => array('id_comments' => array($param['id']))
						)));
		$obj = $obj[0];
		
		$obj->setNew(0);
		CommentsDAL::Save($obj->GetDalDataObj());
		if (Request::IsPost()) {
			$obj->setTitle(Request::GetPost('title', false));
			$obj->setContent(Request::GetPost('content', false));

			CommentsDAL::Save($obj->GetDalDataObj());

			/**
			 * @todo przeniesc ten text w bardziej cywilizowane miejsce
			 */

			$this->AddRedirectInfo(Utils::Translate('Zmiany zostały zachowane',Registry::Get('_user')), true, Router::GenerateUrl('CommentsIndex', array('Comments' => 'Index')), 0);
		}


		$this->smarty->assign('comment', $obj);
	}

	
	public function editModerationAction($param) {
		$dalData = new Comments();
		$obj = CommentsDAL::GetResult($dalData->GetDalDataObj(
								array('condition' => array('id_comments' => array($param['id']))
						)));
		$obj = $obj[0];
		
		$obj->setNew(0);
		CommentsDAL::Save($obj->GetDalDataObj());
		
		if (Request::IsPost()) {
			$obj->setTitle(Request::GetPost('title', false));
			$obj->setContent(Request::GetPost('content', false));

			CommentsDAL::Save($obj->GetDalDataObj());
			$this->approveComment($param['id']);

			/**
			 * @todo przeniesc ten text w bardziej cywilizowane miejsce
			 */

			$this->AddRedirectInfo(Utils::Translate('Zmiany zostały zachowane',Registry::Get('_user')), true, Router::GenerateUrl('CommentsModerationIndex', array('Comments' => 'Moderation')), 0);
		}


		$this->smarty->assign('comment', $obj);
	}
	
	public function settingsAction($param) {
		try {
			if (Request::IsPost()) {
				$dalObj = WpSetupDAL::GetDalDataObj(array('variable' => 'moderation'));
				$obj = WpSetupDAL::GetResult($dalObj);
				$obj = $obj[0];

				$obj->setVariable(Request::GetPost('moderation'));
				WpSetupDAL::Save($obj->getDalDataObj());

				$this->AddRedirectInfo('Zmiany zostały zachowane', 'ok', Router::GenerateUrl('CommentsIndex', array('Comments' => 'Index')), 0);
			}
		} catch (Exception $e) {
			$this->AddRedirectInfo('Wystąpił problem', 'error', Router::GenerateUrl('CommentsIndex', array('Comments' => 'Index')), 0);
		}
	}

	public function ToggleStatusAction($param) {
		$this->SetAjaxRender();

		$dalObj = CommentsDAL::GetDalDataObj(array('id' => $param['id']));
		$this->content = CommentsDAL::DefaultToggleFieldById($dalObj, 'active');
	}

	public function removeAction($param) {
		$this->setNoRender();
		$obj = CommentsDAL::GetResult(
						CommentsDAL::GetDalDataObj(
								array(
									'condition' => array(
										'id' => array($param['id'])
									)
								)
						)
		);

		if (!isset($obj[0])) {
			return $this->AddRedirect(Router::GenerateUrl('commentsIndex', array('Comments' => 'Index')), 0);
		}
		$obj = $obj[0];

		CommentsDAL::DefaultDelete($obj->GetDalDataObj());
		
		$this->addRedirect(Router::Generateurl('commentsIndex',array('comments' => 'index')),0);
	}
	
	/**
	 * usuwanie komentarza z listy moderacji
	 * @param type $idComment 
	 */
	private function approveComment($idComment) {
		$objMod = CommentsAbuseDAL::GetResult(CommentsAbuseDAL::GetDalDataObj(
				array(
									'condition' => array(
										'id_comments' => array($idComment)
									)
								)
				));
		
		if (is_object($objMod[0])) {
			foreach($objMod as $item) {
				CommentsAbuseDAL::DefaultDelete($item->GetDalDataObj());
			}
		}
	}
	
	public function removeFromModerationAction($param) {
		$this->setNoRender();
		$this->approveComment($param['id']);
		$obj = CommentsDAL::GetResult(
						CommentsDAL::GetDalDataObj(
								array(
									'condition' => array(
										'id' => array($param['id'])
									)
								)
						)
		);
		
		$objMod = CommentsAbuseDAL::GetResult(CommentsAbuseDAL::GetDalDataObj(
				array(
									'condition' => array(
										'id_comments' => array($param['id'])
									)
								)
				));
		

		if (is_object($obj[0])) {
			$obj = $obj[0];
			CommentsDAL::DefaultDelete($obj->GetDalDataObj());
		}
		


		
		$this->addRedirect(Router::Generateurl('commentsModerationIndex',array('Comments' => 'Moderation')),0);
	}
	/**
	 * Metoda wspolna
	 *
	 */
	public function preDispatch($param) {
		$this->RunShared('Auth');
		
		$this->Run();
		$this->smarty->assign('param_from_url',$param);
	}

	/**
	 * 
	 *
	 */
	public function postDispatch($param) {
		
	}

}
